package view;

import java.awt.Font;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.ObjectInputStream;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import worldModel.LargeHeal;
import worldModel.MediumHeal;
import worldModel.Navigator;
import worldModel.SmallHeal;

/**
 * The screen that shows up when JavaBoy starts. Should have an option for
 * exiting, starting a new game, and a little eye candy.
 * 
 * @author Cody Mingus
 * 
 */
@SuppressWarnings("serial")
public class TitleView extends MasterViewPanel {

    private JButton newGame;
    private JButton exit;
    private JButton tradeButton;
    private JButton continueButton;

    /**
     * Creates a new TitleView ready to be viewed.
     * 
     * @param m
     */
    public TitleView(MasterView m) {
	super(m);
	setUpLayout();
	setUpButtons();
    }

    /*
     * Set up a grid layout and add our eye candy
     */
    private void setUpLayout() {
	this.setLayout(new GridLayout(5, 1));
	this.add(new JLabel(new ImageIcon("images/javaBoy.png")));
    }

    /*
     * Add the JButtons for exiting and starting a new game.
     */
    private void setUpButtons() {

	Font f = new Font("Segoe UI", 0, 40);

	newGame = new JButton("New Game");
	newGame.addActionListener(new NewGameListener());
	newGame.setFont(f);
	this.add(newGame);

	continueButton = new JButton("Continue");
	continueButton.addActionListener(new ContinueListener());
	continueButton.setFont(f);
	this.add(continueButton);

	tradeButton = new JButton("Trade");
	tradeButton.addActionListener(new TradeListener());
	tradeButton.setFont(f);
	this.add(tradeButton);

	exit = new JButton("Exit");
	exit.addActionListener(new ExitListener());
	exit.setFont(f);
	this.add(exit);
    }

    private class NewGameListener implements ActionListener {

	public void actionPerformed(ActionEvent arg0) {
	    m.changeView(Views.WORLD, new Navigator());
	}

    }

    private class ExitListener implements ActionListener {

	public void actionPerformed(ActionEvent arg0) {
	    System.exit(0);
	}

    }

    private class TradeListener implements ActionListener {

	public void actionPerformed(ActionEvent e) {
	    m.changeView(Views.TRADE, null);
	}

    }

    private class ContinueListener implements ActionListener {

	public void actionPerformed(ActionEvent arg0) {

	    Navigator nav = null;
	    File saveGameFile = new File("navigator.dat");
	    ObjectInputStream ois = null;

	    if (saveGameFile.exists()) {
		try {
		    ois = new ObjectInputStream(new FileInputStream(
			    saveGameFile));
		    nav = (Navigator) ois.readObject();
		    m.changeView(Views.WORLD, nav);
		} catch (FileNotFoundException e) {
		    // Already handled by the if clause
		    e.printStackTrace();
		} catch (IOException e) {
		    // Should not occur
		    e.printStackTrace();
		} catch (ClassNotFoundException e) {
		    e.printStackTrace();
		}

	    } else {
		JOptionPane
			.showMessageDialog(m,
				"Navigator.dat does not exist. This will load a new game.");
		nav = new Navigator();
		nav.addItem(new SmallHeal());
		nav.addItem(new MediumHeal());
		nav.addItem(new LargeHeal());
		m.changeView(Views.WORLD, nav);
	    }
	}
    }

    public String toString() {
	return "TITLE";
    }

}
